const express = require("express")
const mongoose = require("mongoose")
const app = express()


const course_data = [
    {
        id: 2001,
        name: '跟我学理财',
        biglei: '理财',
        minlei: '小白理财',
        zhonglei: "还是理财",
        star: 2.5,
        xueguo: 6455,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '钱在手里没有用，快来理财，钱生钱，老牛皮',
        pinglun: [{
            user: "憨憨",
            content: "这个都是假的，大家快投",
            tx: "/img/kuihua1.jpeg",
            star: 0
        }, {
            user: "洛克",
            content: "洛克王国周年庆，快来快来",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }, {
            user: "迪迦",
            content: "迪迦！！！！！！",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '快来和迪迦一起打怪兽',
                sk: true,
                lei: '凹凸曼'
            },
            {
                type: 'DDV',
                name: '百草枯更胜一筹',
                sk: true,
                lei: '万能小帮手'

            },
            {
                type: '视频',
                name: '小视频',
                sk: false,
                lei: '万能播放器'
            }
        ]
    },
    {
        id: 2002,
        name: '跟我学理财',
        biglei: '理财',
        minlei: '骗子理财',
        zhonglei: "牛逼轰轰",
        star: 1.5,
        xueguo: 6455,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '亿万富豪秒变穷光蛋',
        pinglun: [{
            user: "死托",
            content: "这个理财非常好，真的可以，绝对没有骗人",
            tx: "/img/kuihua1.jpeg",
            star: 0
        }, {
            user: "憨憨",
            content: "我愿拿2楼十年寿命换来一次成功",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }, {
            user: "3楼",
            content: "我觉得2楼说的没毛病",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '迪迦不行，现在都是特利迦',
                sk: true,
                lei: '凹凸曼'
            },
            {
                type: 'DDV',
                name: 'DDVyyds',
                sk: true,
                lei: '万能小帮手'

            },
            {
                type: '视频',
                name: '你想要的都有',
                sk: false,
                lei: '万能播放器'
            }
        ]
    }, {
        id: 1801,
        name: '跟我学理财',
        biglei: '理财ss',
        minlei: '小白理财',
        zhonglei: "也是理财",
        star: 4.5,
        xueguo: 55,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '这个是真的好啊24',
        pinglun: [{
            user: "张三",
            content: "这个课程是骗人的，大家千万不要买",
            tx: "/img/kuihua1.jpeg",
            star: 0
        }, {
            user: "oodjnwu",
            content: "传奇游戏今天开服，是兄弟就来",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }, {
            user: "小明",
            content: "我妈妈拿着旺仔牛奶来看我了",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '课程失误什么什么',
                sk: true,
                lei: '新手地图，和钱做朋友'
            },
            {
                type: 'PDF',
                name: '课程失误什么什么',
                sk: true,
                lei: '新手地图，和钱做朋友'

            },
            {
                type: '视频',
                name: '高级理财什么什么',
                sk: false,
                lei: '高级进阶'
            }
        ]
    },
    {
        id: 1002,
        name: '零基础PPT快速入门—秋叶系列课程',
        biglei: '职业发展',
        minlei: '职业规划',
        zhonglei: "求职应聘",
        star: 3,
        xueguo: 34,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '2017年9月12日-9月22日，4周年重磅回馈5折秒杀，低价史无前例，买到即赚到！',
        pinglun: [{
            user: "李四",
            content: "这个课程还蛮好的，课程一旦购买后，需在规定时间内完成学习，然后提交作业并参加考试，每一科目都完成后才能进入最后的实践课程，即大作业！。",
            tx: "/img/kuihua1.jpeg",
            star: 3
        }, {
            user: "caffhaskda",
            content: "很好",
            tx: "/img/kuihua1.jpeg",
            star: 5
        }, {
            user: "NAGSD",
            content: "求职找到入门的路了",
            tx: "/img/kuihua1.jpeg",
            star: 3.2
        }],
        content: [{
                type: 'PDF',
                name: '跟简七学求职 导学课【购买前必读，免费试学】',
                sk: true,
                lei: '为自己做好保障！'
            },
            {
                type: 'PDF',
                name: '你的目标可以达成吗？',
                sk: true,
                lei: '让你成为求职高手'
            },
            {
                type: '视频',
                name: '教你快速学会几个面试技巧',
                sk: false,
                lei: '高级进阶'
            }
        ]
    },
    {
        id: 1003,
        name: '工作型PPT应该这样做',
        biglei: '办公效率',
        minlei: 'ppt',
        zhonglei: "工作效率",
        star: 4,
        xueguo: 6865,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '课程采用录播视频+线上作业+直播答疑',
        pinglun: [{
            user: "王二",
            content: "真的挺好的，老师讲的好",
            tx: "/img/kuihua1.jpeg",
            star: 2.5
        }, {
            user: "巴拉小仙女",
            content: "老师好帅，讲课特别清楚",
            tx: "/img/kuihua1.jpeg",
            star: 4.5
        }, {
            user: "好好学习",
            content: "推荐你们学",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '让你写出更好的ppt',
                sk: true,
                lei: '给自己一个机会'
            },
            {
                type: 'PDF',
                name: '告诉我你有多努力',
                sk: true,
                lei: '让你成为ppt高手'
            },
            {
                type: '视频',
                name: '教你快速学会几个写ppt的技巧',
                sk: false,
                lei: '高级进阶'
            }
        ]
    },
    {
        id: 1004,
        name: '和我一起学信息图表',
        biglei: '产品和设计',
        minlei: 'Excel',
        zhonglei: "电脑基础",
        star: 4,
        xueguo: 1245,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '课程一旦开始,需要在规定时间内完成学习',
        pinglun: [{
            user: "一飞冲天",
            content: "加油啊，冲啊",
            tx: "/img/kuihua1.jpeg",
            star: 3.5
        }, {
            user: "YYDS",
            content: "一般般吧，有点贵",
            tx: "/img/kuihua1.jpeg",
            star: 2.5
        }, {
            user: "天天向上",
            content: "我天天看，很有用",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '【注意】课程勘误汇总-20221214',
                sk: true,
                lei: '教你学会线性表'
            },
            {
                type: 'PDF',
                name: '让你的页面高端大气',
                sk: true,
                lei: '让你成为信息图表高手'
            },
            {
                type: '视频',
                name: '告别昨日，迎接未来',
                sk: false,
                lei: '高级进阶'
            }
        ]
    },
    {
        id: 1005,
        name: '轻松手绘：用简笔画提升你的竞争力',
        biglei: '页面设计',
        minlei: '漂亮手绘',
        zhonglei: "必要技能",
        star: 4,
        xueguo: 2365,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '教你画出最美最漂亮的图画',
        pinglun: [{
            user: "一个红苹果",
            content: "你是我的小呀小苹果",
            tx: "/img/kuihua1.jpeg",
            star: 3.5
        }, {
            user: "linlin",
            content: "跟着学了，画出来的挺好看的",
            tx: "/img/kuihua1.jpeg",
            star: 5
        }, {
            user: "绘画高手",
            content: "学绘画，+v1234455",
            tx: "/img/kuihua1.jpeg",
            star: 1
        }],
        content: [{
                type: 'PDF',
                name: '注意注意，课程大减价！',
                sk: true,
                lei: '动漫手绘'
            },
            {
                type: 'PDF',
                name: '让你的画栩栩如生',
                sk: true,
                lei: '真人手绘'
            },
            {
                type: '视频',
                name: '怎么快速勾勒线条？',
                sk: false,
                lei: '基本基础'
            }
        ]
    },
    {
        id: 1006,
        name: '让你的宝宝和你更亲近',
        biglei: '亲子教育',
        minlei: 'baby',
        zhonglei: "婴儿小学堂",
        star: 5,
        xueguo: 5565,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '来看看可爱的baby吧',
        pinglun: [{
            user: "梦想成真",
            content: "一个新手妈妈来学学",
            tx: "/img/kuihua1.jpeg",
            star: 3.5
        }, {
            user: "幸福永远",
            content: "教的很有用，觉得宝宝更黏我了",
            tx: "/img/kuihua1.jpeg",
            star: 5
        }, {
            user: "新晋宝妈",
            content: "不知道如何照顾宝宝，来看看",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }],
        content: [{
                type: 'PDF',
                name: '让你的贴心小棉袄更贴心',
                sk: true,
                lei: '对婴儿好的一些小建议'
            },
            {
                type: 'PDF',
                name: '分享一些可爱的宝宝照片',
                sk: true,
                lei: '视觉盛宴'
            },
            {
                type: '视频',
                name: '怎么照顾宝宝？',
                sk: false,
                lei: '照顾宝宝小tips'
            }
        ]
    },
    {
        id: 1007,
        name: '考研小白起步领先营',
        biglei: '学习考试',
        minlei: '考研',
        zhonglei: "大学考试",
        star: 5,
        xueguo: 3465,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '早备考才能拉开你与竞争者的差距',
        pinglun: [{
            user: "学海无涯",
            content: "加油！一定上岸",
            tx: "/img/kuihua1.jpeg",
            star: 4
        }, {
            user: "考神保佑",
            content: "课程不拖拉，讲的都是重点",
            tx: "/img/kuihua1.jpeg",
            star: 5
        }, {
            user: "只想学习",
            content: "2022考研成功！",
            tx: "/img/kuihua1.jpeg",
            star: 4.5
        }],
        content: [{
                type: 'PDF',
                name: '考研英语',
                sk: true,
                lei: '英语语法长难句考点'
            },
            {
                type: 'PDF',
                name: '考研数学',
                sk: true,
                lei: '数学基础入门与避坑指南'
            },
            {
                type: '视频',
                name: '考研政治',
                sk: false,
                lei: '反抗帝国主义的侵略'
            }
        ]
    },
    {
        id: 1008,
        name: '3天播音配音训练营',
        biglei: '兴趣技能',
        minlei: '播音配音',
        zhonglei: "精选",
        star: 5,
        xueguo: 3655,
        coverImg: '/img/ia_100000019.jpg',
        jianjie: '拥有好听的嗓音，提升个人魅力',
        pinglun: [{
            user: "一只努力的果子",
            content: "老师讲的好，实力派",
            tx: "/img/kuihua1.jpeg",
            star: 5
        }, {
            user: "多了一只兔",
            content: "很棒的说，感觉很有帮助",
            tx: "/img/kuihua1.jpeg",
            star: 4.6
        }, {
            user: "沐熙",
            content: "学到了专业知识，老师棒棒的",
            tx: "/img/kuihua1.jpeg",
            star: 4.5
        }],
        content: [{
                type: 'PDF',
                name: '朗诵读诗',
                sk: true,
                lei: '声音美化，塑造好声音'
            },
            {
                type: 'PDF',
                name: '有声书演播',
                sk: true,
                lei: '做有副业的斜杠青年'
            },
            {
                type: '视频',
                name: '声音塑造',
                sk: false,
                lei: '用好声音塑造好形象'
            }
        ]
    },

]






app.use(
    express.urlencoded({
        extended: false,
    })
); // 让我们的项目可以解析url编码的数据
app.use(express.json());
app.use("/", express.static("./public"));

//让express项目可以实现跨域
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});

//数据库操作
//用户表
const users = new mongoose.Schema({
    ziliao: {
        type: Object,
        default: {
            name: '王小美',
            sex: "女"
        }

    },
    account: {
        type: Number,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    toux: {
        type: String,
        default: '/img/kuihua1.jpeg'
    },
    desc: {
        type: String,
        default: '666'
    },
    course: {
        type: Object,
        default: ''
    },
    wenben: {
        type: Object,
        default: [],
    },
    qiandao: {
        type: Object,
        default: ''
    }
}, {
    timestamps: true
})
//分类表 暂时不用
const bigFenlei = new mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    id: {
        type: Number,
        required: true,
        unique: true
    },
    content: {
        type: Object,

    }

}, {
    timestamps: true
})
const minFenlei = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    id: {
        type: Number,
        required: true,


    },
    content: {
        type: Object
    }
}, {
    timestamps: true
})

//数据列表
const itemList = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    id: {
        type: String,
        required: true
    },
    biglei: {
        type: String,
        default: ''
    },
    minlei: {
        type: String,
        default: ''
    },
    star: {
        type: Number,
        default: 0
    },
    xueguo: {
        type: Number,
        default: 0

    },
    jianjie: {
        type: String,
        default: ''
    },
    zhonglei: {
        type: String,
        default: ''
    },
    pinglun: {
        type: Object,
        default: []
    },
    coverImg: {
        type: String,
        default: '/img/kuihua1.jpeg'
    },
    content: {
        type: Object,
        default: []
    }


}, {
    timestamps: true
})
//文章列表
const textList_s = new mongoose.Schema({
    tit: {
        type: String,
        required: true
    },
    id: {
        type: Number,
        required: true
    },
    content: {
        type: String,
        default: ''
    }
}, {
    timestamps: true
})

const Person = mongoose.model("person", users);
const classList = mongoose.model("classList", bigFenlei)
const minList = mongoose.model('minList', minFenlei)
const item = mongoose.model('item', itemList)
const textList = mongoose.model("textList", textList_s)

mongoose.connect("mongodb://localhost:27017/k-user").then(res => {
    console.log("数据库链接成功")
}).catch(err => {
    console.log("数据库链接失败")
    console.log(err)
})
//通过请求初始化外层最大的分类
app.get("/api/l/fenlei/chushi", async (req, res) => {
    /*     const d=[{
            name:'办公效率',
            id:1006,
            content:[{
                name:'办公软件',
                id:2001,
                content:[
                    {
                    name:'ppt',
                    id:30001
                }, {
                    name: 'excel',
                    id: 30002
                }, {
                    name: 'word',
                    id: 30003
                }, {
                    name: 'vue2.0',
                    id: 30004
                }, {
                    name: 'javascript',
                    id: 30005
                },
            ]
            }, {
                name: '工作效率',
                id: 2002,
                content: [{
                    name: 'ppt',
                    id: 30011
                }, {
                    name: 'excel',
                    id: 30012
                }, {
                    name: 'word',
                    id: 30013
                }, {
                    name: 'vue2.0',
                    id: 30014
                }, {
                    name: 'javascript',
                    id: 30015
                }, ]
            }]

        }] */

    const da = [{
        name: '办公效率',
        id: 2001
    }, {
        name: '职业发展',
        id: 2002
    }, {
        name: '编程开发',
        id: 2003
    }, {
        name: '产品和设计',
        id: 2004
    }]
    await classList.insertMany(da).then(r => {
        res.json({
            code: 1,
            tit: '插入成功，请勿重复插入'
        })
    }).catch(e => {
        res.json({
            code: 0,
            tit: 'err'
        })
    })
    let data = await classList.find().then(res => res)
    console.log(data)
})
//通过请求来初始化小分类 小分类的id原则上和外层分类的id对应，方便随时查找
app.get('/api/l/fenlei/chushiMin', async (req, res) => {
    /*  const d=[{
         name:'办公软件',
         id:2001,
         content:[{
             name:'ppt',
             id:90002
         }, {
             name: 'css',
             id: 90003
         }]
     }, {
         name: '办公效率',
         id: 2001,
         content: [{
             name: '时间管理',
             id: 90004
         }]
     }, {
         name: '前端',
         id: 2003,
         content: [{
             name: 'javascript',
             id: 90005
         }]
     }] */
    await minList.insertMany(d).then(r => {
        res.json({
            code: 1,
            tit: "成功了"
        })
    }).catch(e => {
        res.json({
            code: 0,
            tit: '失败了'
        })
    })
    let data = await minList.find().then(res => res)
    console.log(data)
})

//删除分类（用id删除）
app.delete('/api/delete/:id', async (req, res) => {
    if (req.params.id) {
        await classList.findByIdAndDelete(req.params.id).then(r => {
            res.json({
                code: 1
            })
        }).catch(e => {
            res.json({
                code: 0
            })
        })
    }
})
//分类内容查询
app.get('/api/l1/getlei/:id?', async (req, res) => {
    let d = await minList.find().then(r => r).catch(e => false)
    if (!d) {
        res.json({
            code: 0
        })
        return
    }
    if (req.params.id) {
        d = d.filter(item => item.id == req.params.id)
        res.json({
            code: 1,
            data: d
        })

    } else {
        res.json({
            code: 1,
            data: d
        })
    }
})
//插入课程信息
app.post('/api/l1/addItem/', async (req, res) => {
    let p = [{
        name: 'ppt',
        id: 90002,
        content: [{
            name: 'toplun',
            id: 001,
            img: ['./public/img/kuihua1.jpeg', './public/img/kuihua1.jpeg', './public/img/kuihua1.jpeg']
        }, {
            name: '零基础快速入门ppt',

            img: './public/img/kuihua1.jpeg',
            star: 4.5,
            ren: 356,
            price: 55
        }]
    }]
})

//课程数据
app.get('/api/l1/course', async (req, res) => {



    let data = await item.find().then(r => r)
    res.json({
        code: 1,
        data
    })

})


//多功能接口
app.get('/api/l2/course/:id?', async (req, res) => {

   await item.insertMany(course_data).then(r=>{
       res.json({code:1,tit:"插入成功"})
   }).catch(e=>{
       res.json({code:0,tit:"插入失败"})
   })






})



//取最外层分类的api
app.get('/api/l1/lei', async (req, res) => {
    const data = await classList.find().then(r => r).catch(e => false)
    if (data) {
        res.json({
            code: 1,
            data
        })
    } else {
        res.json({
            code: 0,
            data
        })
    }
})


//根据账号id取到某一个用户的信息名字
app.get("/api/l1/User/:id", async (req, res) => {
    Person.count().then((c) => console.log(c));
    /* const p =new Person({
        name:"灰太狼",
        account:2275801328,
        password:"a123456"
    })
    p.save().then(res=>{
        console.log("插入成功")
    }) */
    //根据id去取某一个用户名字
    console.log(req.params)
    await Person.findById('61d2b4237c7f9dcb6a58d0fb').then(data => {
        res.json({
            code: 1,
            tit: '已返回该数据',
            data
        })
    }).catch(err => {
        res.json({
            code: 0,
            tit: '未找到该用户，请检查传入ID是否正确',
            data: {}
        })
    })
});

//注册新增用户
app.post("/api/l1/newUser", async (req, res) => {
    let ac = /^1(3|4|5|6|7|8|9)\d{9}$/
    if (!ac.test(req.body.account)) {
        res.json({
            code: -1,
            tit: '请输入正确的账号'
        })
        return false
    }
    let l = await Person.find().then(r => r)
    if (l.some(item => item.account == req.body.account)) {
        res.json({
            code: 5,
            tit: '账号已经注册了'
        })
        return
    }
    const n = new Person(req.body)
    n.save().then(s => {
        res.json({
            code: 1,
            tit: '注册成功'
        })
    }).catch(e => {
        res.json({
            code: 0,
            tit: '注册失败，请重试或者检查内容格式是否正确'
        })
    })


    console.log(req.body)
})

//登录
app.post("/api/l1/User/login", async (req, res) => {
    console.log(req.body)
    if (!(req.body.account && req.body.password)) {
        res.json({
            code: -1,
            tit: '请输入正确的账号和密码'
        })
        return false
    }
    let data = await Person.find().then(data => data).catch(err => false)
    if (!data) {
        res.json({
            code: null,
            tit: '服务端暂无账号，请注册'
        })
        return false

    }
    let s = data.find(item => item.account == req.body.account && item.password == req.body.password)
    if (s) {
        res.json({
            code: 1,
            tit: '登录成功',
            data: s
        })
    } else {
        res.json({
            code: 0,
            tit: '失败,请检查账号或者是密码是否正确',
            data: undefined,
        })
    }

})
//用户修改资料
app.put('/api/l1/user/updata', async (req, res) => {
    if (!req.body.account) {
        res.json({
            code: -1,
            tit: "修改需要传入账号"
        })
        return false
    }
    let data = await Person.find().then(r => r)
    data = data.find(item => item.account == req.body.account)
    if (!data) {
        res.json({
            code: 0,
            tit: "账号错误"
        })
    }
    let id = data._id

    Reflect.ownKeys(req.body).forEach((item, index) => {
        if (typeof data[item] == 'object') {
            for (let i in req.body[item]) {
                data[item][i] = req.body[item][i]
            }
        } else {
            data[item] = req.body[item]
        }


    })
    await Person.findByIdAndUpdate(id, data).then(r => {
        res.json({
            code: 1,
            tit: "修改成功",
            data
        })
    }).catch(r => {
        res.json({
            code: 0,
            tit: "失败请重试",
        })
    })


})


//用户添加文章
app.post('/api/l1/wenz', async (req, res) => {
    let da = await Person.find().then(r => r)
    da = da.find(item => item.account == req.body.account && item.password == req.body.password)
    if (!da) {
        res.json({
            code: 0,
            tit: '请检查账号密码是否正确'
        })
        return false
    }
    let id = da._id

    da.wenben.push()
    await Person.findByIdAndUpdate(id, da).then(r => {
        res.json({
            code: 1,
            data: r
        })
    }).catch(e => res.json({
        code: 0

    }))

})

//文章新增
app.post('/api/l1/newText', async (req, res) => {

    let data = {}
    let b = new textList({
        ...req.body,
        id: Date.now()
    })
    b.save().then(r => {
        res.json({
            code: 1
        })
    }).catch(e => {
        res.json({
            code: 0
        })
    })
    console.log()

})






app.listen(3002, () => {
    console.log("服务器运行在3002端口");
});